from drf_spectacular.utils import extend_schema, inline_serializer
from rest_framework import status as ResponseStatus
from rest_framework.decorators import action
from rest_framework.mixins import (CreateModelMixin, ListModelMixin,
                                   RetrieveModelMixin, UpdateModelMixin)
from rest_framework.viewsets import GenericViewSet
from backend.cews.services import MaintenanceRecordService
from utils.autoschema import AutoDocSchema, custom_response_serailizer
from utils.decorators import request_data

from backend.cews.models import Log, RepairOrder, RepairOrderRepairDetails, TrainborneMaintenanceRecords

from .serializers import (TrainborneMaintenanceRecordsSerializer)
from rest_framework.response import Response
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination

## Which record MTR old repair order records

class StandardResultsSetPagination(PageNumberPagination):
    page_size = 100
    page_size_query_param = 'page_size'
    page_query_param='page'
    max_page_size = 100

class MaintenanceRecordViewSet(
    RetrieveModelMixin,
    ListModelMixin,
    UpdateModelMixin,
    GenericViewSet,
    CreateModelMixin
):
    serializer_class = TrainborneMaintenanceRecordsSerializer
    queryset = TrainborneMaintenanceRecords.objects.all().order_by('-id')
    # pagination_class = StandardResultsSetPagination

    @action(detail=False, methods=['post'], schema=AutoDocSchema())
    @request_data(keys=["stock_code","serial_no", "complete_status"],optional=["stock_code","serial_no", "complete_status"])
    def get_maintenance_records(self, request, stock_code,serial_no, complete_status):
        status = []
        
        if complete_status:
            status.append(RepairOrder.STATUS.COMPLETE)
            status.append(RepairOrder.STATUS.FORCE_COMPLETE)

        results = MaintenanceRecordService.record_history(stock_code,serial_no,status)

        if results:
            # serializer = TrainborneMaintenanceRecordsSerializer(results, many=True)
            return Response(status=200,
                            data={
                                'code': 1,
                                'data': results
                            })
        
        else:
            return Response(status=200,
                            data={
                                'code': 1,
                                'data': []
                            })
        
    @action(detail=False, methods=['post'], schema=AutoDocSchema())
    @request_data(keys=["main_record_id"])
    def get_maintenance_record(self, request, main_record_id):
        results = MaintenanceRecordService.get_record(main_record_id)
        print(results)
        if results:
            serializer = TrainborneMaintenanceRecordsSerializer(results, many=False)
            return Response(status=200,
                            data={
                                'code': 1,
                                'data': serializer.data
                            })
        
        else:
            return Response(status=200,
                            data={
                                'code': 1,
                                'data': []
                            })
    
    # @action(detail=False, methods=['post'], schema=AutoDocSchema())
    # @request_data(keys=["equipment", "serial_no"])
    # def get_part(self, request, equipment, serial_no):
    #     results = MaintenanceRecordService.get_part(equipment, serial_no)
    #     print(results)
    #     if results:
    #         serializer = TrainborneMaintenanceRecordsSerializer(results, many=False)
    #         return Response(status=200,
    #                         data={
    #                             'code': 1,
    #                             'data': serializer.data
    #                         })
        
    #     else:
    #         return Response(status=200,
    #                         data={
    #                             'code': 1,
    #                             'data': []
    #                         })